/**   
 * @Title: CopyableUtils.java 
 * CopyRight(c)2010-DMX 
 */
package com.dmx.fw.component.util;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.collections.MultiHashMap;
import org.apache.commons.collections.MultiMap;

/** 
 * @Description: CopyableUtils 公用的比较器
 * @author: system
 * @date: 2006
 * @version V1.0 
 * 
 */
public final class CopyableUtils {
	/** 
	 * @Fields DEFAULTARRAY: DEFAULTARRAY数组
	 */
	public static final CopyableInterface[] DEFAULTARRAY=new DefaultCopyable[0];
	
	/** 
	 * @Fields cache: 缓存Map
	 */
    private static final MultiMap cache= new MultiHashMap();
    
    /** 
	 * @Fields globalCache: 全局缓存List
	 */
    private static final List globalCache= new ArrayList();
    
    
    /**
	 * @Description 注册到cache中（Map）
	 * @param copyable: CopyableInterface接口的实现类
	 * @param cls: 类名
	 */
    public static void register(CopyableInterface copyable,Class cls){
    	if(copyable==null || cls==null || "class".equals(cls.getName())) return;
    	cache.put(cls, copyable);
    }
    
    /**
	 * @Description 注册到globalCache中（List）
	 * @param copyable: CopyableInterface接口的实现类
	 */
    public static void register(CopyableInterface copyable){
    	if(copyable==null) return;
    	globalCache.add(copyable);
    }
    
    /**
	 * @Description 判断cache中（包括globalCache）所有CopyableInterface实现类isCopyable
	 * @param propertyName: 
	 * @param propertyValue: 属性值
	 * @param clazz: 类名
	 * @return boolean
	 */
    public static boolean isCopyable(String propertyName, Object propertyValue, Class clazz){
    	Collection collection=(Collection)cache.get(clazz);
    	collection=(collection==null)?new ArrayList():collection;
    	collection.addAll(globalCache);
    	boolean isCopy=true;
    	for(Iterator<CopyableInterface> it=collection.iterator();it.hasNext();){
    		isCopy=isCopy&&it.next().isCopyable(propertyName, propertyValue, clazz);
    		if(!isCopy) return false;
    	}
    	return isCopy;
    }
    
    /**
	 * @Description 获得cache中（包括globalCache）某一Calss的CopyableInterface所有实现类
	 * @param clazz: 类名
	 * @return CopyableInterface[]
	 */
    public CopyableInterface[] getCopyables(Class cls){
    	Collection collection=(Collection)cache.get(cls);
    	collection=(collection==null)?new ArrayList():collection;
    	collection.addAll(globalCache);
    	return (CopyableInterface[])collection.toArray(DEFAULTARRAY);
    }
    
    /**
	 * @Description 默认CopyableInterface的实现类

	 */
    private static class DefaultCopyable implements CopyableInterface{
    	public boolean isCopyable(String propertyName, Object propertyValue,Class clazz) {
    		return true;
    	}
    }
    
    /**
	 * @Description 获得cache中（包括globalCache）某一Calss的CopyableInterface所有实现类
	 * @param clazz: 类名
	 * @return Collection<CopyableInterface>
	 */
    private static Collection<CopyableInterface> getCopyable(Class cls){
    	Collection collection=(Collection)cache.get(cls);
    	collection=(collection==null)?new ArrayList():collection;
    	collection.addAll(globalCache);
    	return collection;
    }
}
